<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {

    public function __construct()  
    {  
        parent::__construct();  
        $res = isLogin();
        if($res == '1')
        {
            $this -> error('还未登录',__APP__.'/Home/Login/index');
        }

        $set = setConfig();

    } 

    public function index(){

        $arr['admin_id'] = session('admin_id');
        if($arr['admin_id'] == '')
        {
            $this -> error('还未登录',__APP__.'/Home/Login/index');
        }
        
        $this->display();
    }

    /*平台信息*/
    public function terrace()
    {
    	$arr['weight'] = C('WEIGHT');
    	$arr['return'] = C('RETURN');
    	$arr['commision'] = C('COMMISION');
    	$arr['buy'] = C('BUY');
        $arr['brokerage'] = C('BROKERAGE');

    	$this->assign('arr' ,$arr);
    	$this->display();
    }

    /*积分充值*/
    public function buy()
    {
        $this->display();
    }
    /*积分充值*/
    public function buyTo()
    {
        $arr['buy'] = C('BUY');
        $arr['type'] = I('post.type');
        $arr['phone'] = I('post.phone');
        $arr['num'] = I('post.num');

        $rechargeModel = D('Recharge');
        $res      = $rechargeModel->buyTo($arr);

        if($res == 'success')
        {
            $this->success('充值成功');
        }
        elseif($res == 'noRecharge')
        {
            $this->error('充值日志增加失败');
        }
        elseif($res == 'noPay')
        {
            $this->error('支付失败');
        }
        elseif($res == 'noShop')
        {
            $this->error('商店不存在！');
        }
    }

    /*新入驻店铺*/
    public function newShop()
    {
    	$shopModel = M('Shop');
    	$shop['isApply'] = 0;
		$count      = $shopModel->where($shop)->count();
		$Page       = new \Think\Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $shopModel->where($shop)->order('shop_id')->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
	    //var_dump($show);exit();
		$this->display(); // 输出模板

    }

    /*新入驻店铺审核通过*/
    public function via($id)
    {
    	$arr['shop_id'] = $id;
    	$shopModel = D('Shop');

    	$res = $shopModel->via($arr);

    	//var_dump($res);exit();
    	if($res)
    	{
    		$this->success('审核成功');
    	}else
    	{
    		$this->error('审核失败！');
    	}
    }

    /*新入驻店铺审核不通过*/
    public function noVia($id)
    {
    	$arr['shop_id'] = $id;
    	$shopModel = D('Shop');
    	$res = $shopModel->noVia($arr);
    	if($res)
    	{
    		$this->success('审核成功');
    	}else
    	{
    		$this->error('审核失败！');
    	}
    }

    /*入驻店铺信息*/
    public function shop()
    {

    	$shopModel = M('Shop');
    	$shop['isApply'] = 1;
		$count      = $shopModel->where($shop)->count();
		$Page       = new \Think\Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $shopModel->where($shop)->order('shop_id')->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
	    //var_dump($show);exit();
    	$this->display();
    }	

    /*提现申请*/
    public function apply()
    {
        $payModel = D('Admin/Pay');
        $res = $payModel->apply();
        
        $this->assign('res',$res);
        $this->display(); 

    }
    /*提现申请审核通过*/
    public function applyVia($id)
    {
        $arr['id'] = $id;
        $payModel = D('Admin/Pay');

        $res = $payModel->via($arr);

        if($res)
        {
            $this->success('审核成功');
        }else
        {
            $this->error('审核失败！');
        }
    }

    /*提现申请审核不通过*/
    public function applyNoVia($id)
    {
        $arr['id'] = $id;
        $payModel = D('Admin/Pay');
        $res = $payModel->noVia($arr);
        if($res)
        {
            $this->success('审核成功');
        }else
        {
            $this->error('审核失败！');
        }
    }

    /*店铺详情*/
    public function more($id)
    {
    	$arr['shop_id'] = $id;
    	$shopModel = D('Shop');
    	$res = $shopModel->getShop($arr, 'shop');
    	
		$this->assign('res',$res);
    	$this->display();
    }

    /*更改店铺*/
    public function set($id)
    {
    	$arr['shop_id'] = $id;
    	$shopModel = D('Shop');
    	$res = $shopModel->getShop($arr, 'shop');
    	
		$this->assign('res',$res);
    	$this->display();
    }
    /*更改店铺*/
    public function setTo()
    {
    	$arr['shop_id']   = trim(I('post.id'));
        $arr['name']   = trim(I('post.name'));
        $arr['contacts']   = trim(I('post.contacts'));
        $arr['area'] = trim(I('post.area'));
        $arr['type']   = trim(I('post.type'));
        $arr['address'] = trim(I('post.address'));
        $arr['shop_points']   = trim(I('post.shop_points'));
        $arr['return_pointd'] = trim(I('post.return_pointd'));
        $arr['synopsis']   = trim(I('post.synopsis'));
        
        $shopModel = D('shop');
        $res = $shopModel->changed($arr);

        if($res) 
        {
        	$this->success('信息修改成功');
        }else
        {
        	$this -> error('信息修改失败');
        }
    }
    
    /*获取上下级*/
    public function level()
    {
    	//获取下级
    	$arr['shop_id'] = I('post.id');

    	$shopModel = D('shop');
        $res = $shopModel->myfans($arr['shop_id'], 'shop');
        $userRes = $shopModel->myfans($arr['shop_id'], 'user');

        //获取店铺的上级
        $shopRes = $shopModel->level($arr['shop_id']);
    	//var_dump($shopRes);exit();
    	$len = count($res);

        $this->assign('shopRes',$shopRes);
        $this->assign('len',$len);
		$this->assign('res',$res);
		$this->assign('userRes',$userRes);
    	$this->display();
    }


    /*删除店铺*/
    public function del($id)
    {
    	$arr['shop_id'] = $id;
    	$shopModel = D('Shop');
    	$res = $shopModel->del($arr);
    	if($res)
    	{
    		$this->success('删除成功');
    	}else
    	{
    		$this->error('删除失败！');
    	}
    }

    /*会员管理*/
    public function user()
    {
    	$userModel = M('user');
		$count      = $userModel->count();
		$Page       = new \Think\Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $userModel->order('user_Id')->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
	    //var_dump($show);exit();
		$this->display(); // 输出模板
    }

    /*会员搜索*/
    public function sousuo()
    {
        $arr['phone'] = trim(I('phone'));

        $userModel = M('user');
        $res = $userModel->where($arr)->select();
        //var_dump($res);exit();
        $this->assign('res' ,$res);
        $this->display();
    }

    /*更改*/
    public function uSet($id)
    {
        $arr['user_id'] = $id;
        $userModel = D('User');
        $res = $userModel->getUser($arr);
        
        $this->assign('res',$res);
        $this->display();
    }
    /*修改会员*/
    public function uSetTo()
    {
        $arr['user_id']   = trim(I('post.id'));
        $arr['pay_points']   = trim(I('post.pay_points'));
        $arr['return_points']   = trim(I('post.return_points'));
        $arr['use_points'] = trim(I('post.use_points'));
        
        $userModel = D('User');
        $res = $userModel->changed($arr);

        if($res) 
        {
            $this->success('信息修改成功');
        }else
        {
            $this -> error('信息修改失败');
        }
    }

    /*会员详情*/
    public function uMore($id)
    {
        $arr['user_id'] = $id;
        $userModel = D('User');
        $res = $userModel->getUser($arr);
        
        $this->assign('res',$res);
        $this->display();
    }

    /*会员删除*/
    public function uDel($id)
    {
        $arr['user_id'] = $id;
        $userModel = D('User');
        $res = $userModel->del($arr);

        if($res)
        {
            $this->success('删除成功');
        }else
        {
            $this->error('删除失败！');
        }
    }

    /*获取上下级*/
    public function uLevel()
    {
        //获取下级
        $arr['user_Id'] = I('post.id');

        $userModel = D( 'Admin/User' );
        $res = $userModel->myfans($arr['user_Id'], 'shop');
        $userRes = $userModel->myfans($arr['user_Id'], 'user');

        $shopRes = $userModel->level($arr['user_Id']);
        $len = count($res);
       // var_dump($shopRes);exit();

        $this->assign('shopRes',$shopRes);
        $this->assign('len',$len);
        $this->assign('res',$res);
        $this->assign('userRes',$userRes);
        $this->display();
    }

    /*地区列表*/
    public function place()
    {
        $userModel = D( 'Seller/Area' );
        $res = $userModel->index('area');
        //var_dump($res);exit;
        $this->assign('res',$res);
        $this->display();
    }

    /*修改地区*/
    public function areaSet($id)
    {
        //var_dump($area);exit;
        $userModel = D( 'Seller/Area' );
        $res = $userModel->one($id, 'area');
        $this->assign('res',$res);
        $this->display();
    }

    /*修改地区*/
    public function areasetTo()
    {
        $area['area_id'] = I('post.id');
        $area['name'] = trim(I('post.name'));

        $areaModel = D( 'Seller/Area' );
        $res = $areaModel->set($area, 'area');
        if($res)
        {
            $this->success('修改成功');
        }else
        {
            $this->error('修改失败！');
        }
    }
    /*地区删除*/
    public function aDel($id)
    {
        $arr['area_id'] = $id;
        $areaModel = D( 'Seller/Area' );
        $res = $areaModel->del($arr, 'area');

        if($res)
        {
            $this->success('删除成功');
        }else
        {
            $this->error('删除失败！');
        }
    }

    /*增加*/
    public function areaAdd()
    {
        $area['name'] = I('post.name');
        $areaModel = D( 'Seller/Area' );
        $res = $areaModel->areaAdd($area, 'area');

        if($res)
        {
            $this->success('增加成功');
        }else
        {
            $this->error('增加失败！');
        }
    }

    /*商品分类列表*/
    public function cla()
    {
        $userModel = D( 'Seller/Class' );
        $res = $userModel->index('class');
        //var_dump($res);exit;
        $this->assign('res',$res);
        $this->display();
    }

    /*修改商品分类*/
    public function classSet($id)
    {
        $userModel = D( 'Seller/Class' );
        $res = $userModel->one($id, 'class');
        
        $this->assign('res',$res);
        $this->display();
    }

    /*修改商品分类*/
    public function classsetTo()
    {
        $class['id'] = I('post.id');
        $class['name'] = trim(I('post.name'));
        //var_dump($class);exit;
        $areaModel = D( 'Seller/Class' );
        $res = $areaModel->set($class, 'class');
        if($res == 'notIn')
        {
            $this->error('修改失败！');
        }else
        {
            $this->success('修改成功');
        }
    }
    /*商品分类删除*/
    public function cDel($id)
    {
        $arr['class_id'] = $id;
        $areaModel = D( 'Seller/Class' );
        $res = $areaModel->del($arr, 'class');

        if($res)
        {
            $this->success('删除成功');
        }else
        {
            $this->error('删除失败！');
        }
    }

    /*增加商品分类*/
    public function classAdd()
    {
        $class['name'] = I('post.name');

        $areaModel = D( 'Seller/Class' );
        $res = $areaModel->areaAdd($class, 'class');

        if($res == 'notIn')
        {
            $this->error('增加失败！');
        }else
        {
            $this->success('增加成功');
        }
    }

    /*修改密码*/
    public function pwd()
    {
        $arr['admin_id'] = session('admin_id');

        $adminModel = M( 'admin' );
        $res = $adminModel->where($arr)->select();

        $this->assign('res', $res);
        $this->display();
    }

    /*修改密码*/
    public function pwdTo()
    {
        $old['pwd'] = md5(trim(I('post.mpass')));
        $arr['newpass'] = md5(trim(I('post.newpass')));
        $new['pwd'] = md5(trim(I('post.renewpass')));

        
        $adminModel = M('admin');
        $adminRes = $adminModel->where($old)->select();
        if($adminRes)
        {
            $adminId['admin_id'] = $adminRes[0]['admin_id'];
            //var_dump($adminId);exit();
            $save = $adminModel->data($new)->where($adminId)->save();
            if($save)
            {
                $this->success('修改成功');
            }else
            {
                $this->error('修改失败');//密码修改失败
            }
        }else
        {
            $this->error('旧密码错误');//旧密码错误
        }

    }

    /*提现记录*/
    public function applyPay()
    {
        //获取商家提现
        $payModel = D('Admin/Pay');
        $res = $payModel->applyPay();
        
        $this->assign('res',$res);
        $this->display(); 

    }

    /*打印充值记录*/
    public function dayin()
    {
        $payModel = D('Admin/Pay');
        $res = $payModel->pay();
        
        $a = $this->excle($res);
        $b = substr($a, 1);
        header('Location:' . $b);
        //$this->success('导出成功!', __APP__.'/Home/Index/applyPay');
    }

     public function excle($order) {
        $table_head = array('姓名','联系方式', '充值金额', '充值时间', '充值人类型');
        $table_name = './' . time() . '.xls';
        $table_file = fopen($table_name, 'w');

        foreach ($table_head as $str) {
            $table_heads.=iconv('utf-8', 'gb2312', $str) . chr(9);
        }
        fwrite($table_file, $table_heads);
        foreach ($order as &$rows) {
            $table_line = chr(13);
            $table_line.=iconv('utf-8', 'gb2312', $rows['contacts']) . chr(9);
            $table_line.=$rows['phone'] . chr(9);
            $table_line.=iconv('utf-8', 'gb2312', $rows['money']) . chr(9);
            $table_line.=date('Y-m-d H:i:s', $rows['shijian']) . chr(9);
            $table_line.=iconv('utf-8', 'gb2312', $rows['type']) . chr(9);
            
            fwrite($table_file, $table_line);
        }
        fclose($table_file);
        return $table_name;
    }


    /*充值记录*/
    public function pay()
    {
        //获取商家提现
        $payModel = D('Admin/Pay');
        $res = $payModel->pay();
        //var_dump($res);exit;
        $this->assign('res',$res);
        $this->display(); 
    }

    /*平台信息修改*/
    public function setTerrace()
    {
        $arr['weight'] = C('WEIGHT');
        $arr['return'] = C('RETURN');
        $arr['commision'] = C('COMMISION');
        $arr['buy'] = C('BUY');
        $arr['brokerage'] = C('BROKERAGE');

        $this->assign('arr' ,$arr);
        $this->display();
    }
    public function setTreeaceTo()
    {
        $arr['weight'] = trim(I('post.weight'));
        $arr['return'] = trim(I('post.return'));
        $arr['commision'] = trim(I('post.commision'));
        $arr['buy'] = trim(I('post.buy'));
        $arr['brokerage'] = trim(I('post.brokerage'));
        
        $config['Id'] = 1;
        $configModel = M('config');
        $save = $configModel->data($arr)->where($config)->save();
        
        if($save)
        {
            $this->success('修改成功');
        }else
        {
            $this->error('修改失败');//密码修改失败
        }


    }


}